package com.jt.service;

import com.jt.mapper.UserMapper;
import com.jt.pojo.User;
import com.jt.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService{

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> findAll() {
        return userMapper.findAll();
    }

    /**
     * 思路：
     *  user数据：User(id=null, username=asd, password=123, phone=null, email=null, status=null)
     *  1.将密码明文，转换为密位  sha1/md5算法/md5hash
     *  2.根据用户名密码查询用户信息
     *  3.有结果：返回token
     *  4.没有结果：返回null
     * @param user
     * @return
     */
    @Override
    public String login(User user) {
        String password = user.getPassword();
        byte[] bytes = password.getBytes();
        //将密码加密
        String md5Password = DigestUtils.md5DigestAsHex(bytes);
        user.setPassword(md5Password);
        //根据用户名和密码查询数据库
        System.out.println(user);
        User userDB = userMapper.findUserByUP(user);
        //3.判断userDB是否有值
        if(userDB!=null){
            String token = UUID.randomUUID().toString();
            return token;
        }else{
            //用户名密码错误
            return null;
        }

    }

    /**
     * 分页的sql：
     *      select * from user limit 起始位置,查询条数
     *      第一页：每页10条
     *      select * from user limit 0,10
     *      第二页：每页10条
     *      select * from user limit 10,10
     *      第n条
     *      select * from user limit (n-1)*10,10
     * @param pageResult
     * @return
     */
    @Override
    public PageResult findUserList(PageResult pageResult) {
        //总记录数
        long total = userMapper.findTotal();
        //获取分页查询的结果
        int start = (pageResult.getPageNum() - 1)*pageResult.getPageSize();
        int size = pageResult.getPageSize();
        String query = pageResult.getQuery();
        List<User> rows = userMapper.findUserList(start,size,query);
        return pageResult.setTotal(total).setRows(rows);
    }

    /**
     * 需求：修改我们的状态信息
     * 参数：id/status/update
     * @param user
     */
    @Override
    @Transactional
    public void updateStatus(User user) {
        user.setUpdated(new Date());
        userMapper.updateStatus(user);
    }

    @Override
    @Transactional
    public void deleteUserById(User user) {
        userMapper.deleteUserById(user);

    }

    /**
     * 业务：
     *      1.密码需要加密MD5
     *      2.设定状态码的信息
     *      3.设定创建时间和修改时间
     *
     * @param user
     */
    @Override
//    @Transactional(rollbackFor = Exception.class) //事务的注解
    @Transactional
    public void saveUser(User user) {
        //将我们的密码加密处理
        String password = user.getPassword();
        String md5Pass = DigestUtils.md5DigestAsHex(password.getBytes());
        user.setPassword(md5Pass).setStatus(true).
                setCreated(new Date()).
                setUpdated(user.getCreated());
        userMapper.saveUser(user);
    }

    @Override
    @Transactional
    public User findUserUpById(User user) {
        return userMapper.findUserUpById(user);
    }

    @Override
    @Transactional
    public void userUpdateById(User user) {
        user.setUpdated(new Date());
        userMapper.userUpdateById(user);
    }


}
